<?php
// Configuration area start
// Please config the following settings before translation besides Traditional Chinese :)
// Notice :
// 1. Give rights to web server for writing all language files if you are not use M$ OS
// ex. /language/  and  /modules/*/language/
// 2. Please put this script to the root path of your SugarCRM installation

$bk = chr(10);     // The sign of line break
$qo = '  ';        // The sign for quote
$lang = 'zh_cn';   // Your language code in ISO-XXX
$encode = 'UTF-8'; // The encode you want to use

// Configuration area end
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="<?php echo $lang; ?>">

  <head>
	<meta http-equiv="Content-Type" content="text/html; charset=<?php echo $encode; ?>" />
	</head>
	<body>
<form method="get">
<select name="kfolder">	
<?php
$dir = "modules"; 
if ($dh = opendir($dir)) {
    while (($folder = readdir($dh)) !== false) { 
      if(is_dir($dir.'/'.$folder)&&$folder!='..'&&$folder!='.') {
        echo '<option';
        if(isset($_GET['kfolder']) && $folder == $_GET['kfolder'])
          echo ' selected';
        echo '>'.$folder.'</option>';
      }
    } 
    closedir($dh); 
} 
?>
</select><input type="submit">
</form>

<?php

if(isset($_GET['kfolder']) && $_GET['kfolder']){
  echo '<form method="post">';
  if(isset($_POST['go']) && $_POST['go']){
    $string = '';
	@unlink('modules/'.$_GET['kfolder'].'/language/'.$lang.'.lang.php_old');
    @copy('modules/'.$_GET['kfolder'].'/language/'.$lang.'.lang.php', 'modules/'.$_GET['kfolder'].'/language/'.$lang.'.lang.php_old');
    $fd = fopen('modules/'.$_GET['kfolder'].'/language/'.$lang.'.lang.php', 'w');
    fwrite($fd, '<?php'.$bk.'$mod_strings = array ('.$bk);
    //$string .= 'app_list_strings = array ('.$bk;
    foreach($_POST['mod_strings'] as $key1 => $arr){
      if(is_array($arr)){
        $string .= $qo.'\''.$key1.'\' =>'.$bk.$qo.'array ('.$bk;
        foreach($arr as $key2 => $string1){
          $string .= $qo.$qo.'\''.$key2.'\' => \''.$string1.'\','.$bk;
        }
        $string .= $qo.'),'.$bk;
      } else {
        $string .= $qo.'\''.$key1.'\' => \''.$arr.'\','.$bk;
      }
    }
    fwrite($fd, $string);
    $string = '';
    if(isset($_POST['mod_list_strings']) && sizeof($_POST['mod_list_strings']) > 0){
    	fwrite($fd, $bk.');'.$bk.'$mod_list_strings = array ('.$bk);
      foreach($_POST['mod_list_strings'] as $key1 => $arr){
        if(is_array($arr)){
          $string .= $qo.'\''.$key1.'\' =>'.$bk.$qo.'array ('.$bk;
          foreach($arr as $key2 => $string1){
            $string .= $qo.$qo.'\''.$key2.'\' => \''.$string1.'\','.$bk;
          }
          $string .= $qo.'),'.$bk;
        } else {
          $string .= $qo.'\''.$key1.'\' => \''.$arr.'\','.$bk;
        }
      }
      fwrite($fd, $string);
    }
    fwrite($fd, $bk.');'.$bk.'?>');
	fclose($fd);
  }
  

  include 'modules/'.$_GET['kfolder'].'/language/en_us.lang.php';
  global $mod_strings;
  global $mod_list_strings;

  $en_mod_strings = $mod_strings;
  $en_mod_list_strings = $mod_list_strings;
  if(file_exists('modules/'.$_GET['kfolder'].'/language/'.$lang.'.lang.php')) {
    include 'modules/'.$_GET['kfolder'].'/language/'.$lang.'.lang.php';
	global $mod_strings;
    global $mod_list_strings;
  }

  $tmpArray2 = array();
  foreach($mod_strings as $key2 => $value2) {
	if(array_key_exists($key2,$en_mod_strings)) {
		$tmpArray2[$key2] = $value2;
	}
  }
  $mod_strings = $tmpArray2;
  $result1 = array_merge($en_mod_strings, $mod_strings);

  $tmpArray = array();
  if(isset($mod_list_strings) && is_array($mod_list_strings) && isset($en_mod_list_strings) && is_array($en_mod_list_strings)) {
	  foreach ($mod_list_strings as $key => $value) {
		if(array_key_exists($key,$en_mod_list_strings)) {
			$valueArray = array();
			if(is_array($value)) {
				foreach($value as $key1 => $value1) {
					if(array_key_exists($key1,$en_mod_list_strings[$key])) {
						$valueArray[$key1] = $value1;
					}
				}
				$tmpArray[$key] = $valueArray;
			} else {
				$tmpArray[$key] = $value;
			}
		}
	  }
  }
  $mod_list_strings = $tmpArray;
  $result2 = array_merge($en_mod_list_strings, $mod_list_strings);
  foreach($result1 as $key => $arr){
    if(is_array($arr)){
      foreach($arr as $key2 => $string){
        echo '<input type="text" name="mod_strings['.$key.']['.$key2.']" value="'.$string.'" size="80"><br>';
      }
    } else {
      echo '<input type="text" name="mod_strings['.$key.']" value="'.$arr.'" size="80"><br>';
    }
  }
  if($result2){
    foreach($result2 as $key => $arr){
      if(is_array($arr)){
        foreach($arr as $key2 => $string){
          echo '<input type="text" name="mod_list_strings['.$key.']['.$key2.']" value="'.$string.'" size="80"><br>';
        }
      } else {
        echo '<input type="text" name="mod_list_strings['.$key.']" value="'.$arr.'" size="80"><br>';
      }
    }
  }
  echo '<input type="submit" name="go" value="go">';
  echo '</form>';
}
?>
</body>
</html>